function restClient(url,successFun,data,isAsync,contentType){
    $.ajax({
        type:'POST',
        url:url,
        beforeSend:function(jqXHR) {
            jqXHR.setRequestHeader("sessionId", $.cookie("sessionId"));
            var h = document.body.clientHeight;
            $("<div class=\"datagrid-mask\"></div>").css({display:"block",width:"100%",height:h}).appendTo("body");
            $("<div class=\"datagrid-mask-msg\"></div>").html("正在努力加载数据，请稍候。。。").appendTo("body").css({display:"block",
                left:($(document.body).outerWidth(true) - 190) / 2,
                top:(h - 45) / 2});
        },
        complete: function(){
            $('.datagrid-mask-msg').remove();
            $('.datagrid-mask').remove();
        },
        data:data,
        dataType:'json',
        success:successFun,
        async:isAsync,
        contentType:contentType
    });
}

function isLogon() {
    var sessionId = $.cookie("sessionId");
    if(sessionId) {
        return true;
    } else {
        location.href = 'login.html';
    }
}

function cleanCookie(){
    $.cookie("sessionId", null);
    $.cookie("managerId", null);
    $.cookie("managerName", null);
    $.cookie("roleId", null);
    $.cookie("roleName", null);
    $.cookie("roleLevel", null);
    $.cookie("appId", null);
    $.cookie("pageSize", null);
}

Date.prototype.Format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};

function formatStatus(value,row,index){
    if(row.status != null) {
        if(row.status === -1){
            return '禁用';
        } else if(row.status === -2){
            return '<span style="color:red">删除</span>';
        } else if(row.status === 0) {
            return '启用';
        }
    }
}

function formatCity(value,row,index){
    if(row.cityId != null){
        var cityName = '';
        restClient('/rest/manager/city/'+row.cityId, function(data){
            cityName = data[0].name;
        }, null, false);
        return cityName;
    }
}

function formatMsgType(value,row,index) {
    if(row.msgType) {
        if(row.msgType == "text") {
            return "文本消息";
        } else if(row.msgType == "news") {
            return "图文消息";
        } else if(row.msgType == "event") {
            return "事件消息";
        } else {
            return row.msgType;
        }
    }
}

function loadCityCombobox(combo, needSetDefault){
    restClient('/rest/manager/allcity', function(data){
        if(data) {
            combo.combobox('loadData', data);
            if(needSetDefault) {
                combo.combobox("setValue", data[0].id);
            }
        }
    });
}

function loadAppIdCombobox(combo, needSetDefault){
    restClient('/rest/appsecret/listEnable', function(data){
        if(data) {
            combo.combobox('loadData', data);
            if(needSetDefault) {
                combo.combobox("setValue", data[data.length - 1].appId);
            }
        }
    });
}

function formatAppId(row) {
    var s = '<span style="font-weight:bold">' + row.description + '</span><br/>' +
        '<span style="color:#888">' + row.appId + '</span>';
    return s;
}


//格式化日期
Date.prototype.format = function(format){
    var o = {
        "M+" : this.getMonth()+1, //month
        "d+" : this.getDate(), //day
        "h+" : this.getHours(), //hour
        "m+" : this.getMinutes(), //minute
        "s+" : this.getSeconds(), //second
        "q+" : Math.floor((this.getMonth()+3)/3), //quarter
        "S" : this.getMilliseconds() //millisecond
    }

    if(/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    }

    for(var k in o) {
        if(new RegExp("("+ k +")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
        }
    }
    return format;
};